﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BLL;
namespace KMServer
{
    public partial class LogicAPI
    {
        UserCenter userCenter = new UserCenter();
        public Dictionary<string, string> Register(Dictionary<string, string> p)
        {
            bool status=userCenter.Register(p["username"],p["password"],int.Parse(p["script_id"]));
            Dictionary<string, string> result = new Dictionary<string, string>();
            result.Add("status", status.ToString().ToLower());
            if (status)
            {
                result.Add("message", "注册成功了！");
            }
            else
            {
                result.Add("message", "用户名已经存在！");
            }
            result.Add("msg_key", p["msg_key"]);
            return result;
        }
        public Dictionary<string, string> Login(Dictionary<string, string> p)
        {
            string loginKey ;
            Model.tb_user currentUser = userCenter.Login(p["username"], p["password"], int.Parse(p["script_id"]),out loginKey);
            Dictionary<string, string> result = new Dictionary<string, string>();
            bool flag = loginKey != "";
            if (flag)
            {
                result.Add("status","true" );
                result.Add("user_name",currentUser.user_name);
                result.Add("server_time",DateTime.Now.ToString());
                result.Add("end_time",currentUser.expiration_time.ToString());
                result.Add("login_key",loginKey);
                result.Add("message","登录成功");
            }
            else
            {
                result.Add("status", "false");
                result.Add("message","用户名或密码错误");
            }
            result.Add("msg_key", p["msg_key"]);
            return result;
        }
        public Dictionary<string, string> Logout(Dictionary<string, string> p)
        {
      
            bool flag = userCenter.Logout(p["login_key"]);
            Dictionary<string, string> result = new Dictionary<string, string>();
        
            if (flag)
            {
                result.Add("status", "true");
                result.Add("message", "注销成功");
            }
            else
            {
                result.Add("status", "false");
                result.Add("message", "注销失败");
            }
            result.Add("msg_key", p["msg_key"]);
            return result;
        }
        public Dictionary<string, string> Recharge(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
             KeyValuePair<bool,string> temp = userCenter.Recharge(p["cdkey"],p["login_key"]);
            result.Add("status", temp.Key.ToString().ToLower());
            result.Add("message", temp.Value);
            result.Add("msg_key", p["msg_key"]);
            return result;
        }
        public Dictionary<string, string> ResetPassword(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
             KeyValuePair<bool,string> temp = userCenter.ResetPassword(p["old_password"],p["new_password"],p["login_key"]);
            result.Add("status", temp.Key.ToString().ToLower());
            result.Add("message", temp.Value);
            result.Add("msg_key", p["msg_key"]);
            return result;
        } public Dictionary<string, string> Heartbeat(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            Model.tb_user user1;
             KeyValuePair<bool,string> temp = userCenter.Heartbeat(p["login_key"],out user1);
            if (temp.Key)
            {
                result.Add("server_time", DateTime.Now.ToString());
                result.Add("end_time", user1.expiration_time.ToString());

            }
            result.Add("status", temp.Key.ToString().ToLower());
            result.Add("message", temp.Value);
            result.Add("msg_key", p["msg_key"]);
            return result;
        }



        MUserCenter mUserCenter = new MUserCenter();
        public Dictionary<string, string> MHeartbeat(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            Model.tb_m_user user1;
            KeyValuePair<bool, string> temp = mUserCenter.Heartbeat(p["login_key"], out user1);
            result.Add("status", temp.Key.ToString().ToLower());
            result.Add("message", temp.Value);
            result.Add("msg_key", p["msg_key"]);
            return result;
        }
        public Dictionary<string, string> GetAllKey(Dictionary<string, string> p)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            KeyValuePair<bool, string> temp = mUserCenter.GetAllKey(p["login_key"]);
            if (temp.Key)
            {
                result.Add("message", temp.Value);
            }
            else
            {
                result.Add("message", temp.Value);
            }
            result.Add("status", temp.Key.ToString().ToLower());

            result.Add("msg_key", p["msg_key"]);
            return result;
        }
        public Dictionary<string, string> MLogin(Dictionary<string, string> p)
        {
            string loginKey;
            Model.tb_m_user currentUser = mUserCenter.Login(p["username"], p["password"], out loginKey);
            Dictionary<string, string> result = new Dictionary<string, string>();
            bool flag = loginKey != "";
            if (flag)
            {
                result.Add("status", "true");
                result.Add("login_key", loginKey);
                result.Add("message", "登录成功");
            }
            else
            {
                result.Add("status", "false");
                result.Add("message", "用户名或密码错误");
            }
            result.Add("msg_key", p["msg_key"]);
            return result;
        }
    }
}
